package gavdi.dk.controller;

import gavdi.dk.data.DAOCustomer;
import gavdi.dk.model.AbsenceRegistration;
import gavdi.dk.model.Event;
import gavdi.dk.model.TimeRegistration;
import gavdi.dk.util.Tools;
import gavdi.dk.view.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestListener;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;





public class EventAdapter extends ArrayAdapter<Event>{

    Context context; 
    int layoutResourceId;    
    static List<Event> events;
    protected static List<Event> testList;
    

    private static class EventHolder {
    	int position;
        TextView txtTitle;
        TextView txtTitle2;
        TextView txtTitle3;
        TextView txtTitle4;
        TextView txtTitle5;
        

    }
    

    
    public EventAdapter(Context context, int layoutResourceId, List<Event> events) {
        super(context, layoutResourceId, events);
        System.out.println("test1");
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        EventAdapter.events = events;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	System.out.println("getView " + position + " " + convertView);

        EventHolder holder;
        
    
 
        if(convertView == null)
        {
        	
		//	LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        	LayoutInflater inflater = LayoutInflater.from(context); 
        	convertView = inflater.inflate(layoutResourceId, null);

            holder = new EventHolder();
            holder.txtTitle = (TextView)convertView.findViewById(R.id.txtTitle);
            holder.txtTitle2 = (TextView)convertView.findViewById(R.id.txtTitle2);
            holder.txtTitle3 = (TextView)convertView.findViewById(R.id.txtTitle3);
            holder.txtTitle4 = (TextView)convertView.findViewById(R.id.txtTitle4);
            holder.txtTitle5 = (TextView)convertView.findViewById(R.id.txtTitle5);
            
            holder.position = position;

            


  
            convertView.setTag(holder);
        }
        else {
        	holder = (EventHolder) convertView.getTag();
        }
		
		

      
        new EventTask(position, holder)
        .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);



     
        	return convertView;
        	
        	
    }
    
    private static class EventTask extends AsyncTask {
        private int ePosition;
        private EventHolder eHolder;
        

        public EventTask(int position, EventHolder holder) {
            ePosition = position;
            eHolder = holder;
        }

   

		@Override
		protected Object doInBackground(Object... params) {
		
	

			
			return null;
		}
		
		@Override
		protected void onPostExecute(Object result) {
		      // if (eHolder.position == ePosition) {
		    	
			
		
				Event e = events.get(ePosition);
			
			

				if (e.getType().equalsIgnoreCase("T")){
					TimeRegistration tr = (TimeRegistration) e;
	 				eHolder.txtTitle.setText(tr.getCustomer().getName());
					eHolder.txtTitle2.setText(tr.getCustomer().getWbs());
					
				}
				else if (e.getType().equalsIgnoreCase("A")){
					AbsenceRegistration ar = (AbsenceRegistration) e;
					eHolder.txtTitle.setText(ar.getAbsence().getName());
					eHolder.txtTitle2.setText(ar.getAbsence().getCode());
				}
				
		    	   
		    	   
		    	   eHolder.txtTitle3.setText(Tools.dateSplitter(e.getDate()));
		    	   eHolder.txtTitle4.setText(e.getStatus());
		    	   eHolder.txtTitle5.setText(e.getComment());

		    	   
		    	
		       //}
		       
	//	       System.out.println(eHolder.txtTitle.getText().toString() + ePosition+" "+ePosition);


		}





    }

    
    

  
    
}